package com.zlm.app.emp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zlm.app.emp.service.UserEmpService;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.userEmp.OrgParamsDto;
import com.zlm.common.dto.userEmp.PositionParamsDto;
import com.zlm.common.entity.UserTokenDO;
import com.zlm.common.enums.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页-controller
 * @Author: duyufu
 * @Date: 2019/4/24 10:57
 */
@RestController
@RequestMapping(value = "/homePage")
public class HomePageController extends CheckController {

    @Autowired
    private UserEmpService userEmpService;

    /**
     * 获取轮播图
     * @param type
     * @return
     */
    @GetMapping("/getSysBannerByType")
    public JsonResultDto getSysBannerByType(Integer type) {
        try {
            JsonResultDto result = userEmpService.getSysBannerByType(type);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取行业
     * @return
     */
    @GetMapping("/getAllIndustry")
    public JsonResultDto getAllIndustry() {
        try {
            JsonResultDto result = userEmpService.getAllIndustry();
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取职位列表
     * @param positionParamsDto
     * @return
     */
    @PostMapping("/getPositionList")
    public JsonResultDto getPositionList(PositionParamsDto positionParamsDto) {
        try {
            JsonResultDto result = userEmpService.getPositionList(positionParamsDto);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取职位详情
     * @param pracId
     * @return
     */
    @GetMapping("/getPosition")
    public JsonResultDto getPosition(String token, Integer pracId) {
        try {
            if (null == pracId) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            JsonResultDto result = userEmpService.getPosition(token, pracId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取公司详情
     * @param orgId
     * @return
     */
    @GetMapping("/getSysOrg")
    public JsonResultDto getSysOrg(String token, Integer orgId) {
        try {
            if (null == orgId) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            JsonResultDto result = userEmpService.getSysOrg(token,orgId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 查询所有城市
     * @return
     */
    @GetMapping("/queryAllCity")
    public JsonResultDto queryAllCity(){
        try {
            JsonResultDto result = userEmpService.queryAllCity();
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 查询地级市
     * @return
     */
    @GetMapping("/queryPrefectureLevelCity")
    public JsonResultDto queryPrefectureLevelCity(){
        try {
            JsonResultDto result = userEmpService.queryPrefectureLevelCity();
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }

    }

    /**
     * 获取筛选条件
     * @return
     */
    @GetMapping("/getOtherRequire")
    public JsonResultDto getOtherRequire() {
        try {
            //获取企业规模
            List orgScaleList = CompanyScaleEnum.getCompanyScale();
            //获取职位性质
            List workNatureList = JobNatureEnum.getJobNature();
            //获取学历要求
            List educationList = EducationEnum.getEducation();
            //获取工作经验
            List workTimeList = ExperienceEnum.getWorkYear();
            //获取企业性质
            List orgNatureList = CompanyNatureEnum.getCompanyNature();
            //获取企业行业
//            JsonResultDto result = userEmpService.getAllIndustry();

            List<Map> requireList = new ArrayList<>();
            Map<String, Object> objMap = new HashMap<>(3);
            objMap.put("key", "workNature");
            objMap.put("name", "职位性质");
            objMap.put("data", workNatureList);
            requireList.add(objMap);
            objMap = new HashMap<>(3);
            objMap.put("key", "education");
            objMap.put("name", "学历要求");
            objMap.put("data", educationList);
            requireList.add(objMap);
            objMap = new HashMap<>(3);
            objMap.put("key","workTime");
            objMap.put("name","工作经验");
            objMap.put("data", workTimeList);
            requireList.add(objMap);
            objMap = new HashMap<>(3);
            objMap.put("key","orgNature");
            objMap.put("name","企业性质");
            objMap.put("data", orgNatureList);
            requireList.add(objMap);
            objMap = new HashMap<>(3);
            objMap.put("key","orgScale");
            objMap.put("name","企业规模");
            objMap.put("data", orgScaleList);
            requireList.add(objMap);
            return success(requireList);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 收藏/取消收藏职位
     * @param token
     * @param operationCode
     * @param pracId
     * @return
     */
    @GetMapping("/saveEmpPostCollect")
    public JsonResultDto saveEmpPostCollect(String token, Integer operationCode, Integer pracId) {

        try {
            if (null == operationCode || null == pracId) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            //更新收藏操作
            JsonResultDto result = userEmpService.saveEmpPostCollect(operationCode,pracId,userToken.getUserId());
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 收藏/取消收藏公司
     * @param token
     * @param operationCode
     * @param orgId
     * @return
     */
    @GetMapping("/saveEmpOrgCollect")
    public JsonResultDto saveEmpOrgCollect(String token, Integer operationCode, Integer orgId) {
        try {
            if (null == operationCode || null == orgId) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            //更新收藏操作
            JsonResultDto result = userEmpService.saveEmpOrgCollect(operationCode, orgId, userToken.getUserId());
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 添加/删除职业顾问
     * @param token
     * @param operationCode
     * @param partId
     * @return
     */
    @GetMapping("/editLocalPartner")
    public JsonResultDto editLocalPartner(String token, Integer operationCode, Integer partId) {
        try {
            if (null == operationCode || null == partId) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.editLocalPartner(operationCode,partId,userToken.getUserId());
            checkJsonResultDto(result);
            return success();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 分页获取职业顾问列表
     * @param pageNumber
     * @param pageSize
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    @GetMapping("/queryLocalPartner")
    public JsonResultDto queryLocalPartner(Integer pageNumber, Integer pageSize, String token, Double lng, Double lat) {
        try {
            JsonResultDto result = userEmpService.queryLocalPartner(pageNumber,pageSize, token, lng, lat);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 查询我的职业顾问
     * @param token
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/queryMyPartner")
    public JsonResultDto queryMyPartner(String token, Integer pageNumber, Integer pageSize) {
        try {
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.queryMyPartner(userToken.getUserId(),pageNumber,pageSize);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 分页获取明星企业
     * @param orgParamsDto
     * @return
     */
    @PostMapping("/queryStarEnterprise")
    public JsonResultDto queryStarEnterprise(OrgParamsDto orgParamsDto) {
        try {
            JsonResultDto result = userEmpService.queryStarEnterprise(orgParamsDto);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 报名
     * @param pracId
     * @param token
     * @return
     */
    @GetMapping("/empSignUp")
    public JsonResultDto empSignUp(Integer pracId, String token) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.empSignUp(pracId,userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }
}
